//
//  SLCommonsDateUtility.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/5/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "SLCommonsDateUtility.h"

@implementation SLCommonsDateUtility

+ (NSDate *)currentGMTDate
{
    NSDate *currentDate = [NSDate date];
    return [SLCommonsDateUtility localDateToGMTDate:currentDate];
}

+ (NSDate *)localDateToGMTDate:(NSDate *)localDate 
{
    NSTimeZone *sourceTimeZone = [NSTimeZone systemTimeZone];
    NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:localDate];
    NSInteger destinationGMTOffset = [gmtTimeZone secondsFromGMTForDate:localDate];
    
    NSTimeInterval timeInterval = destinationGMTOffset - sourceGMTOffset;
    
    return [[[NSDate alloc] initWithTimeInterval:timeInterval sinceDate:localDate] autorelease];
}

+ (NSDate *)GMTDateToLocalDate:(NSDate *)gmtDate
{
    NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
    
    NSInteger gmtOffset = [gmtTimeZone secondsFromGMTForDate:gmtDate];
    NSInteger destinationOffset = [destinationTimeZone secondsFromGMTForDate:gmtDate];
    
    NSTimeInterval timeInterval = destinationOffset - gmtOffset;
    
    return [[[NSDate alloc] initWithTimeInterval:timeInterval sinceDate:gmtDate] autorelease];
}

+ (NSDateFormatter *)localDefaultDateTimeFormat:(SLCommonsDateUtilityDateTimeFormat)dateFormatterType
{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.locale = [NSLocale systemLocale];
    
    if (SLCommonsDateUtilityDateTimeFormatShortFormat == dateFormatterType) {
        dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:kSRDateFormatterWithShortFormat options:0 locale:[NSLocale systemLocale]];
    } else if (SLCommonsDateUtilityDateTimeFormatMediumFormat == dateFormatterType) {
        dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:kSRDateFormatterWithMediumFormat options:0 locale:[NSLocale systemLocale]];
    } else {
        dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:kSRDateFormatterWithLongFormat options:0 locale:[NSLocale systemLocale]];
    }
    
    return [dateFormatter autorelease];
}

@end
